package fy.uap.operatorImpl;

import java.util.ArrayList;
import java.util.List;

import org.apache.commons.logging.LogFactory;

import com.seeyon.oainterface.exportData.flow.FlowExport;
import com.seeyon.oainterface.exportData.form.ValueExport;

import fy.form.listen.log.LogWriter;
import fy.form.listen.operate.AForm_Save_Operator;
import fy.form.listen.set.OA_UAPAccountContrast;
import fy.uap.dao.SupplierXiaDanDao;
import fy.util.MyUtil;

public class SupplierXiaDanFormOperator extends AForm_Save_Operator {

	private static org.apache.commons.logging.Log log = LogFactory
			.getLog(SupplierXiaDanFormOperator.class);

	private static final String formName = "潜在供应商ERP增加下单次数申请表";

	private String pk_corp;

	private String supplierCode;

	private List<String> frequencyList = new ArrayList<String>();

	private List<String> rangeList = new ArrayList<String>();

	// private String memberCode;
	//
	// private String applicationTime;
	//
	private String number;
	//
	private long memberID;

	public String getFormName() {
		return formName;
	}

	@Override
	protected void parseOtherField_FirstStep(com.seeyon.v3x.services.flow.bean.FlowExport flowExport) {
		memberID = flowExport.getFlowCreatePerson().getId();
		//
		// memberCode = MyOrgManager.getInstance().getMemberByID(memberID)
		// .getCode();
		// applicationTime = flowExport.getFlowCreateTime();
		// applicationTime = applicationTime.substring(0,
		// applicationTime.indexOf('.'));
	}

	@Override
	protected void parseValueExport(com.seeyon.v3x.services.form.bean.ValueExport ve) {
		String name = ve.getDisplayName();
		String value = ve.getValue();

		if ("单位选择器".equals(name)) {
			pk_corp = OA_UAPAccountContrast.getUapAccountID(value);
		} else if ("供应商编码".equals(name)) {
			supplierCode = value;
		} else if ("单据编号".equals(name)) {
			number = value;
		} else if ("供货范围PK".equals(name)) {
			rangeList.add(value);
		} else if ("次数".equals(name)) {
			frequencyList.add(value);
		}
	}

	@Override
	protected boolean save() {
		ProviderMessageManager pmm = new ProviderMessageManager();
		pmm.setMemberID(memberID);
		if (rangeList.size() > 0 && rangeList.size() == frequencyList.size()) {
			if (MyUtil.strIsNotNull(supplierCode)
					&& MyUtil.strIsNotNull(pk_corp)) {

				SupplierXiaDanDao dao = new SupplierXiaDanDao();
				dao.setNumber(number);
				dao.setFrequencyList(frequencyList);
				dao.setRangeList(rangeList);
				dao.setSupplierCode(supplierCode);
				dao.setPk_corp(pk_corp);

				if (dao.save()) {
					return true;
				} else {
					StringBuilder sb = new StringBuilder().append("表单《")
							.append(formName)
							.append("》数据导入到ERP系统发生异常，请手工导入。单据编号：")
							.append(number);
					pmm.sendMessage(sb.toString());
					log.error(sb.toString());
					LogWriter.writeLog(MyUtil.getRealPath(this.getClass()),
							sb.toString());
				}
			} else {
				StringBuilder sb = new StringBuilder().append("获取表单《")
						.append(formName)
						.append("》供应商名称、编码或者所在子公司为空，无法自动导入，请用户手工导入。单据编号：")
						.append(number);
				pmm.sendMessage(sb.toString());
				log.error(sb.toString());
				LogWriter.writeLog(MyUtil.getRealPath(this.getClass()),
						sb.toString());
			}

		} else {
			StringBuilder sb = new StringBuilder().append("获取表单《")
					.append(formName)
					.append("》的供货范围和下单次数发生异常，表单数据导入到UAP失败，请手工导入。单据编号：")
					.append(number);
			pmm.sendMessage(sb.toString());
			log.error(sb.toString());
			LogWriter.writeLog(MyUtil.getRealPath(this.getClass()), sb.toString());
		}
		return false;
	}
}
